<?php
/*
Plugin Name: Useful Functions
Plugin URI: http://un-petit-p.eu
Description: This plugin contains useful functions
Author: olivM
Version: 0.1
Author URI: http://un-petit-p.eu
*/

function post_is_cat($postID, $category) {
	foreach(get_the_category($postID) as $cat)
		if ($cat->cat_ID == $category || $cat->category_nicename == $category)
			return true;
}

function get_cat_by_slug($slug = '') {
	global $wpdb;

	$key = 'category_'.$slug;
	if ( ! $category = wp_cache_get($key, 'ladiesroom_categories') ) {
		$sql = "SELECT * FROM ".$wpdb->categories." WHERE category_nicename = '".$slug."' ";
		$category = $wpdb->get_row($sql);	

		wp_cache_add($key, $category, 'ladiesroom_categories');
	}
	
	return $category;
}

function get_catID_by_slug($slug = '') {
	global $wpdb;

	$category = get_cat_by_slug($slug);
	return $category->cat_ID;
}

function errorsToSession() {
	global $error, $errors;
	
	// Incase a plugin uses $error rather than the $errors array
	if ( !empty( $error ) ) {
		$errors['error'] = $error;
		unset($error);
	}
	$_SESSION['errors'] = $errors;
	
}

function truncate($sstring,$llength) {
// --------------------------------------------------------------
    if (strlen($sstring)>$llength) {
        return substr($sstring,0,$llength)."...";
    } else {
        return $sstring;
    }
}

function log_r($stuff) {
	if (is_array($stuff) || is_object($stuff))
		error_log(print_r($stuff,true));
	else
		error_log($stuff);
}
?>
